package com.example.recyclerviewfenzu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.recyclerviewfenzu.Test.SectionTestEntity;
import com.example.recyclerviewfenzu.Test.TestAdapter;
import com.example.recyclerviewfenzu.Test.TestEntity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView reclclerview;

    private TestAdapter mTestAdapter;
    private ArrayList<TestEntity.ResultBean> mResult=new ArrayList<>();
    private ArrayList<SectionTestEntity> sectionTestEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        reclclerview = findViewById(R.id.reclclerview);


        initData();
        initView();
    }


    /**
     * 模拟后台数据返回
     */
    private void initData() {
        for (int i = 0; i < 2; i++) {
            TestEntity.ResultBean resultBean = new TestEntity.ResultBean();
            resultBean.setTitle("我有一只小狗" + i);
            List<TestEntity.ResultBean.ListBean> list = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                TestEntity.ResultBean.ListBean listBean = new TestEntity.ResultBean.ListBean();
                listBean.setMessage("我有一只小狗我有一只小狗我有一只小狗我有一只小狗" + j);
                list.add(listBean);
            }
            resultBean.setList(list);
            mResult.add(resultBean);
        }
    }

    private void initView() {
        //初始化的时候默认没有数据，显示空的布局
        sectionTestEntity=new ArrayList<>();
        for(int i=0;i<mResult.size();i++){
            SectionTestEntity header=new SectionTestEntity(true,mResult.get(i).getTitle(),true);
            sectionTestEntity.add(header);
            for(int j=0;j<mResult.get(i).getList().size();j++){
                SectionTestEntity content=new SectionTestEntity(mResult.get(i).getList().get(j));
                sectionTestEntity.add(content);
            }
        }
        refreshView();
    }


    /**
     * 刷新消息列表
     */
    private void refreshView() {
        //1,加载空布局文件，便于第五步适配器在没有数据的时候加载
        View emptyView = View.inflate(this, R.layout.empty_view, null);
        //2，设置LayoutManager,LinearLayoutManager表示竖直向下
        reclclerview.setLayoutManager(new LinearLayoutManager(this));
        //3，初始化一个无数据的适配器
        mTestAdapter = new TestAdapter(R.layout.item_test_list,R.layout.item_test_head,sectionTestEntity,this);
        //4，绑定recyclerView和适配器
        reclclerview.setAdapter(mTestAdapter);
        //5，给recyclerView设置空布局
        mTestAdapter.setEmptyView(emptyView);
        //6，给recyclerView的每一个子列表添加点击事件，在内部区分是不是头部或者子列表
        mTestAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SectionTestEntity s=sectionTestEntity.get(position);
                if(s.isHeader){
                    Toast.makeText(MainActivity.this, "点击了头部第"+position+s.header,
                            Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this, "点击了第子条目"+position+s.t.getTest(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        mTestAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "点击了第Mors"+position+"onItemChildClick" + position, Toast.LENGTH_LONG).show();
            }
        });
    }
}
